<?php

namespace apiend\modules\test\controllers;

use Yii;
use apiend\components\Controller;
use common\models\base\Customer;

/**
 * RedisController for the `test` module
 */
class RedisController extends Controller {

    /**
     * Renders the index view for the module
     * @return string
     */
    public function actionIndex() {
        Yii::beginProfile('countryQueryCost');
        $begintime = microtime(true);
        $model = new Customer();
//
        for ($i = 0; $i < 1000; $i++) {
            $_model = clone $model;
//            $_model->setAttributes([
//                'name' => 'ttt_' . $i,
//                'address' => Yii::$app->request->userIP,
//                'registration_date' => date('Y-m-d H:i:s'),
//                'status' => 0,
//            ]);
            $_model->name = 'ttt_' . $i;
            $_model->address = Yii::$app->request->userIP;
            $_model->registration_date = date('Y-m-d H:i:s');
            $_model->status = 0;
            $_model->save();
//            $model->setAttributes($attributes);
//            $model->name = 'ttt_' . $i;
//            $model->address = Yii::$app->request->userIP;
//            $model->registration_date = date('Y-m-d H:i:s');
//            $model->status = 0;
//            $model->save() && $model->id = 0;
//            $model->isNewRecord = true;
        }

        $data = Customer::find()->where(['status' => 0])->all();
        Yii::endProfile('countryQueryCost');



        return microtime(true) - $begintime;
//        return $data;

        $key = 'u_1';

        $redis = new Redis();

        $redis->connect('127.0.0.1', 6379);

        $redis->set('my_php_redis', 'install phpredis');

        return $redis->get('my_php_redis');

        /* @var $redis \common\components\db\RedisConnection */
        $redis = Yii::$app->redis;
        //$redis->hmset($key, 'id', 1, 'username', 'test');
        $redis->hmset($key, [
            'id' => 2,
            'username' => 'demo',
        ]);

//        $key = 'u_2';
//        $redis->setArray($key, [
//            'id' => 2,
//            'username' => 'demo',
//        ]);

        $data['hgetall'] = $redis->hgetall($key);
        $data['hget_id'] = $redis->hget($key, 'id');
        $data['hget_username'] = $redis->hget($key, 'username');
        return $data;
    }

}
